package com.open.cloud.rbac.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.inspur.micro.core.response.ResponseWrapper;
import com.open.cloud.rbac.object.user.CoreUserDto;
import com.open.cloud.rbac.object.user.CoreUserSelectDto;
import com.open.cloud.rbac.object.user.CoreUserVo;
import com.open.cloud.rbac.service.user.ICoreUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wangzengzhou01
 * @since 2024-12-23
 */
@Api(tags = "RBAC服务-用户业务")
@Slf4j
@RestController
@RequestMapping(value = "api/v1/core/user")
public class CoreUserController {

    @Resource
    private ICoreUserService userService;

    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    public ResponseWrapper<IPage<CoreUserVo>> getUserVoPage(@RequestBody CoreUserSelectDto selectDto) {
        IPage<CoreUserVo> data = userService.getUserByPage(selectDto);
        return ResponseWrapper.success(data);
    }

    @ApiOperation(value = "创建")
    @PostMapping("/create")
    public ResponseWrapper createUserInfo(@RequestBody CoreUserDto userDto) {
        JSONObject data = userService.createUserInfo(userDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

    @ApiOperation(value = "更新")
    @PostMapping("/update")
    public ResponseWrapper updateUserInfo(@RequestBody CoreUserDto userDto) {
        JSONObject data = userService.changeUserInfo(userDto);
        String message = data.getString("message");
        Integer code = data.getInteger("code");
        return code == 200 ? ResponseWrapper.success(message) : ResponseWrapper.failure(code, message);
    }

}

